import sdk from "../../index";
import base_component from "../../../component/base_component";
import dieReward_component from "../../../component/dieReward_component";
import rpg_component from "../../../component/rpg_component";
import { config } from "../../../config";
import { ComType } from "../../tool/component";
import { IBodyCreateConfig } from "../types";
import { BaseBody } from "./body_base";
export default class enemy extends BaseBody {
    constructor() {
        super();
        this.id = 'E' + sdk.counter.get('enemy');
        console.log('创建敌人',this.id);
    }
    create(config: IBodyCreateConfig) {
        let con_base = this.getComponent(ComType.base) as base_component;
        con_base.nickName = config.name;
        con_base.emoji = config.emoji;
        con_base.show = config.url;
        let con_rpg = this.getComponent(ComType.rpg) as rpg_component;
        con_rpg.config_setUp(config);
    }
    // 读取配置表 随机
    randomConfig(table: string) {
        let key = `enemy_${table}`;
        let dieCom = this.getComponent(ComType.死亡掉落) as dieReward_component;
        dieCom.cfg_table = key;

        let cfg_list = config.get(key) as Map<string, any>;
        let keys = Array.from(cfg_list.keys());
        let randomKey = keys[Math.floor(Math.random() * keys.length)];
        let cfg = cfg_list.get(randomKey);
        dieCom.cfg_id = randomKey;

        let _skill = [];
        // 确保技能配置是数组格式
        if (cfg.skill && Array.isArray(cfg.skill)) {
            for (let i = 0; i < cfg.skill.length; i++) {
                let skill = cfg.skill[i] as any;
                let skillName = skill.name || undefined;
                _skill.push({ skillId: skill.id, cooldown: 0, reName: skillName, data: {} });
            }
        } else {
            // 如果没有技能配置，添加默认的普通攻击技能
            _skill.push({ skillId: 'A000', cooldown: 0, reName: '普通攻击', data: {} });
        }
        let _attribute = [];
        // 属性字符串格式 最大生命值-物理攻击
        if(cfg.attribute.length == 0){
            debugger;
        }
        for (let i = 0; i < cfg.attribute.length; i++) {
            let attribute = cfg.attribute[i] as any;
            _attribute.push({ attribute: attribute.key, value: Number(attribute.val) });
        }
        let createCfg = {
            url: 'https://dew-1251587647.cos.ap-guangzhou.myqcloud.com/show_player/1.png',
            emoji: cfg.emoji,
            name: cfg.name,
            level: 1,
            skill: _skill,
            attribute: _attribute
        }
        this.create(createCfg);
    }
}